package xin.petstore.framework.utils;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**   
* @Title: IPUtils.java 
* @Package
* @Description: 获取远程用户登录ip
* @author
* @date 2016年5月16日 上午10:15:36 
* @version V1.0   
*/
public class IPUtils {


	public static String localIP=null;

	private static final String SEPARATORS = ",";
	private static String localIp;
	private static String localPort;
	private static final String[] HEADERS_TO_TRY = { 
        "X-Forwarded-For",
        "Proxy-Client-IP",
        "WL-Proxy-Client-IP",
        "HTTP_X_FORWARDED_FOR",
        "HTTP_X_FORWARDED",
        "HTTP_X_CLUSTER_CLIENT_IP",
        "HTTP_CLIENT_IP",
        "HTTP_FORWARDED_FOR",
        "HTTP_FORWARDED",
        "HTTP_VIA",
        "REMOTE_ADDR",
        "X-Real-IP"};
	
	/**
     * 获取客户端ip地址(可以穿透代理)
     * 
     * @param request
     * @return
     */
    public static String getRemoteAddr(HttpServletRequest request) {
    	
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if (ip.contains(SEPARATORS)) {
        	 final String[] arr = ip.split(SEPARATORS);
             for (final String str : arr) {
                 if (!"unknown".equalsIgnoreCase(str)) {
                     ip = str;
                     break;
                 }
             }
		}
       
        return ip;
    }
    
   

	/**
	 * 获取节点IP
	 * 
	 * @author dingzhanhe
	 * @return
	 */
	public static String getLocalIp() {
		if (localIp != null) {
			return localIp;
		}
		localIp = getVmParamValue("localip");
		return localIp;
	}

	/**
	 * 获取节点端口
	 *
	 * @author dingzhanhe
	 * @return
	 */
	public static String getLocalPort() {
		if (localPort != null) {
			return localPort;
		}
		localPort = getVmParamValue("localport");
		return localPort;
	}

	/**
	 * 根据参数名，从虚拟机启动参数列表中获取参数值，如果参数为空则返回null；如果无此参数也返回null;
	 *
	 * @author dingzhanhe
	 * @param paramName
	 * @return
	 */
	public static String getVmParamValue(String paramName) {
		if (paramName == null || paramName.trim().equals("")) {
			return null;
		}
		return System.getProperty(paramName, null);
	}

	/**
	 * 获取本地的IP地址
	 * @return
     */
	public static String getLocalIP(){
		if(localIP==null){
			try {
				localIP = InetAddress.getLocalHost().getHostAddress();
			} catch (UnknownHostException e) {
				localIP="Unknown IP";
				e.printStackTrace();
			}
		}
		return localIP;
	}

	/**
	 * 获取用户的IP地址
	 * @param request
	 * @return
     */
	public static String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}

		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}

		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

}
